package com.dk.hi.gui.comp;

import java.util.ArrayList;
import java.util.List;

import org.newdawn.slick.GameContainer;
import org.newdawn.slick.Graphics;
import org.newdawn.slick.SlickException;
import org.newdawn.slick.state.StateBasedGame;

import com.dk.hi.gui.Message;

public abstract class GraphicContainer extends GraphicComponent {
	private List<GraphicComponent> children; // the component children

	public GraphicContainer() {
		this.children = new ArrayList<GraphicComponent>();
	}

	// Adds a child component
	public void addChild(GraphicComponent comp) {
		// First we initialize the component container in order for it to
		// communicate with the container
		comp.setContainer(this);
		this.children.add(comp);
	}

	// Method handling message reception from children
	public abstract void receiveMessage(GraphicComponent from, Message msg);

	// Renders the component and it's children
	@Override
	public void render(Graphics g) throws SlickException {
		// Call of the render method
		renderComponent(g);

		// Call of the render method for each chil
		for (GraphicComponent child : this.children) {
			child.renderComponent(g);
		}
	}

	// Lunches the update process for the component and it's children
	@Override
	public void update(GameContainer container, StateBasedGame game, int delta) throws SlickException {
		// Call of the update method overrided in subclass
		updateComponent(container, game, delta);

		// Call the update for each child
		for (GraphicComponent child : this.children) {
			child.updateComponent(container, game, delta);
		}
	}
}
